package com.fnzx.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fnzx.entity.ScoreEntity;
import com.fnzx.service.ScoreService;
import com.fnzx.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.*;


/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-10-23 22:20:00
 */
@RestController
@Api(tags = "评分")
@RequestMapping("iot/score")
public class ScoreController {
    @Autowired
    private ScoreService scoreService;

    @ApiOperation(value = "个人顾问用户自评",notes = "需要传入createdId scoreId scoreDetails")
    @PostMapping("/selfJudge")
    public R selfJudge(@RequestBody ScoreEntity scoreEntity){
        scoreEntity.setSocreSource(1000);
        if (scoreService.save(scoreEntity)) {
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "查询出项目评分",notes = "查询出项目评分createdId=1,scoredId=1454744356559839233")
    @GetMapping("/listScorePro")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "scoredId",value = "项目id",paramType = "query",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "createdId",value = "操作人id",paramType = "query",required = true,dataType = "Long")
    })
    public R listScorePro(@RequestParam(value = "scoredId") Long scoredId, @RequestParam(value = "createdId") Long createdId) {
        ScoreEntity scoreEntity = scoreService.getByScoreAndUser(scoredId,createdId);
        return R.ok().put("scoreEntity",scoreEntity);
    }


    @ApiOperation(value = "添加项目评分",notes = "添加项目评分(createdId->评分人id，socreSource固定传入1001表示项目评分，scoredId表示项目id){\n" +
            "    \"createdId\": \"1\",\n" +
            "    \"scoreDetails\": \"{\\\"detail\\\": \\\"备注字段\\\", \\\"1454737599594803201\\\": \\\"1\\\", \\\"1454737645417574402\\\": \\\"1\\\", \\\"1454737672206594049\\\": \\\"1\\\", \\\"1454737690023997442\\\": \\\"1\\\", \\\"1454744356559839233\\\": \\\"1\\\"}\",\n" +
            "    \"scoredId\": \"1454744356559839233\",\n" +
            "    \"socreSource\": 1001\n" +
            "}")
    @PostMapping("/saveScorePro")
    public R saveScorePro(@RequestBody ScoreEntity scoreEntity) {
        ScoreEntity scoreAndUser = scoreService.getByScoreAndUser(scoreEntity.getScoredId(), scoreEntity.getCreatedId());
        if (scoreAndUser == null) {
            scoreService.save(scoreEntity);
            return R.ok();
        }
        return R.ok("不能重复评分");
    }


    @ApiOperation(value = "修改项目评分",notes = "修改项目评分(id->表示要修改的id,createdId->评分人id，socreSource固定传入1001表示项目评分，scoredId表示项目id){\n" +
            "    \"createdId\": \"1\",\n" +
            "    \"id\": \"2\",\n" +
            "    \"scoreDetails\": \"{\\\"detail\\\": \\\"备注字段\\\", \\\"1454737599594803201\\\": \\\"1\\\", \\\"1454737645417574402\\\": \\\"1\\\", \\\"1454737672206594049\\\": \\\"1\\\", \\\"1454737690023997442\\\": \\\"1\\\", \\\"1454744356559839233\\\": \\\"1\\\"}\",\n" +
            "    \"scoredId\": \"1454744356559839233\",\n" +
            "    \"socreSource\": 1001\n" +
            "}")
    @PostMapping("/updateScorePro")
    public R updateScorePro(@RequestBody ScoreEntity scoreEntity) {
        scoreService.updateById(scoreEntity);
        return R.ok();
    }

    @ApiOperation("个人评分")
    @PostMapping("/personalScored")
    public R personalScored(@RequestBody ScoreEntity scoreEntity){
        scoreEntity.setSocreSource(1002);
        if (scoreService.save(scoreEntity)) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "企业评分",notes = "需要传入createdId scoreId scoreDetails")
    @PostMapping("/enterpriseScored")
    public R enterpriseScored(@RequestBody ScoreEntity scoreEntity){
        scoreEntity.setSocreSource(1003);
        scoreService.save(scoreEntity);
        return R.ok();
    }

    @ApiOperation(value="个人用户测评",notes = "需要传入createdId scoreId scoreDetails\n" +
            "{\n" +
            "  \"createdId\": 1,\n" +
            "  \"scoreDetails\": {\n" +
            "    \"score\": [\n" +
            "      {\n" +
            "        \"title\": \"评测点1\",\n" +
            "        \"value\": 3\n" +
            "      },\n" +
            "      {\n" +
            "        \"title\": \"评测点2\",\n" +
            "        \"value\": 2\n" +
            "      },\n" +
            "      {\n" +
            "        \"title\": \"评测点3\",\n" +
            "        \"value\": 3\n" +
            "      },\n" +
            "      {\n" +
            "        \"title\": \"评测点4\",\n" +
            "        \"value\": 5\n" +
            "      },\n" +
            "      {\n" +
            "        \"title\": \"评测点5\",\n" +
            "        \"value\": 2\n" +
            "      },\n" +
            "      {\n" +
            "        \"title\": \"评测点6\",\n" +
            "        \"value\": 1\n" +
            "      },\n" +
            "      {\n" +
            "        \"title\": \"评测点7\",\n" +
            "        \"value\": 4\n" +
            "      },\n" +
            "      {\n" +
            "        \"title\": \"评测点8\",\n" +
            "        \"value\": 2\n" +
            "      },\n" +
            "      {\n" +
            "        \"title\": \"评测点9\",\n" +
            "        \"value\": 3\n" +
            "      },\n" +
            "      {\n" +
            "        \"title\": \"评测点10\",\n" +
            "        \"value\": 3\n" +
            "      }\n" +
            "    ]\n" +
            "  },\n" +
            "  \"scoredId\": 1454354589506646018,\n" +
            "}")
    @PostMapping("/testScored")
    public R testScored(@RequestBody ScoreEntity scoreEntity){
        scoreEntity.setSocreSource(1004);
        scoreService.save(scoreEntity);
        return R.ok();
    }

    @ApiOperation("按照评分人查找评分详情集合")
    @GetMapping("/listByCreatedId")
    public R listByCreatedId(@RequestParam Long createdId){
        QueryWrapper<ScoreEntity> wrapper = new QueryWrapper<ScoreEntity>().eq("created_id", createdId);
        List<ScoreEntity> list = scoreService.list(wrapper);
        return R.ok().put("list",list);
    }

    @ApiOperation("按照被评分人的查找评分详情集合")
    @GetMapping("/listByScoredId")
    public R listByScoredId(@RequestParam Long scoredId){
        QueryWrapper<ScoreEntity> wrapper = new QueryWrapper<ScoreEntity>().eq("scored_id", scoredId);
        List<ScoreEntity> list = scoreService.list(wrapper);
        return R.ok().put("list",list);
    }
}
